﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;

namespace Bunbyx.game
{
    // --------------------------------------------------------------------
    //  Enumerations
    // --------------------------------------------------------------------

    public enum Music
    {
        Background
    }

    public enum Sound
    {
        Jump
    }

    // --------------------------------------------------------------------
    //  Sound Player
    // --------------------------------------------------------------------

    public class SoundPlayer
    {
        // --------------------------------------------------------------------
        //  Constants
        // --------------------------------------------------------------------

        private const string BG_MUSIC = "Sounds/BunbyxTheme";
        
        private const string JUMP_SOUND = "Sounds/jump";

        public float MUSIC_VOLUME = 0.3f;
        public float EFFECTS_VOLUME = 0.2f;

        // --------------------------------------------------------------------
        //  Private attributes
        // --------------------------------------------------------------------

        // Music Management
        private Dictionary<Sound, SoundEffect> sounds;
        private Dictionary<Music, SoundEffect> musics;
        private SoundEffectInstance musicInstance;

        // --------------------------------------------------------------------
        //  Constructor
        // --------------------------------------------------------------------

        public SoundPlayer()
        {
            sounds = new Dictionary<Sound, SoundEffect>();
            musics = new Dictionary<Music, SoundEffect>();
            musicInstance = null;
        }

        // --------------------------------------------------------------------
        //  Public methods
        // --------------------------------------------------------------------

        public void LoadContent(ContentManager content)
        {
            // Load musics
            musics.Add(Music.Background, content.Load<SoundEffect>(BG_MUSIC));

            // Load sounds
            sounds.Add(Sound.Jump, content.Load<SoundEffect>(JUMP_SOUND));
        }

        public void PlayMusic(Music music)
        {
            // Stop previous music
            StopMusic();

            // Play new music
            musicInstance = musics[music].CreateInstance();
            musicInstance.IsLooped = true;
            musicInstance.Volume = MUSIC_VOLUME;
            musicInstance.Play();
        }

        public void StopMusic()
        {
            if (musicInstance != null)
            {
                musicInstance.Stop();
                musicInstance = null;
            }
        }

        public void PlaySound(Sound sound)
        {
            sounds[sound].Play(EFFECTS_VOLUME, 0f, 0f);
        }
    }
}
